package com.itcp.itelpicture.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcp.itelpicture.common.BaseResponse;
import com.itcp.itelpicture.common.DeleteRequest;
import com.itcp.itelpicture.common.Result;
import com.itcp.itelpicture.domain.dto.community.CommentAddRequest;
import com.itcp.itelpicture.domain.dto.community.CommentQueryRequest;
import com.itcp.itelpicture.domain.vo.communityVo.CommentVO;
import com.itcp.itelpicture.exception.BusinessException;
import com.itcp.itelpicture.exception.ErrorCode;
import com.itcp.itelpicture.service.ICommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 评论区接口
 * 
 * @author wgzzz
 */
@RestController
@RequestMapping("/comment")
@Slf4j
public class CommentController {

    @Resource
    private ICommentService commentService;

    
    /**
     * 新增评论
     * 
     * @param commentAddRequest 创建请求
     * @return 评论ID
     */
    @PostMapping("/add")
    public BaseResponse<Long> addComment(@RequestBody CommentAddRequest commentAddRequest) {
        if (commentAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long result = commentService.addComment(commentAddRequest);
        return Result.success(result);
    }

    /**
     * 删除
     * 
     * @param deleteRequest 删除请求
     * @return 是否成功
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteComment(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = commentService.deleteComment(deleteRequest.getId());
        return Result.success(result);
    }

    /**
     * 分页获取一级评论列表（封装类）
     * 
     * @param commentQueryRequest 查询条件
     * @return 分页结果
     */
    @PostMapping("/list/root/vo")
    public BaseResponse<Page<CommentVO>> rootCommentVOByPage(
            @RequestBody CommentQueryRequest commentQueryRequest) {
        if (commentQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<CommentVO> commentVOPage = commentService
                .rootCommentVOByPage(commentQueryRequest);
        return Result.success(commentVOPage);
    }

    /**
     * 分页获取一级评论列表（封装类）
     *
     * @param commentQueryRequest 查询条件
     * @return 分页结果
     */
    @PostMapping("/list/reply/vo")
    public BaseResponse<Page<CommentVO>> replyCommentVOByPage(
            @RequestBody CommentQueryRequest commentQueryRequest) {
        if (commentQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<CommentVO> commentVOPage = commentService
                .replyCommentVOByPage(commentQueryRequest);
        return Result.success(commentVOPage);
    }

}